package com.hiwan.system.util;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类DateUtil.java的实现描述：日期转换
 *
 */
public class DateUtil {

    /**
     * 说明：计算两个时间差:分为秒，分，时，天，月，年
     *
     * @param timeStart long
     * @param timeEnd   long
     * @return returnStr 返回时间差字符
     * @time 2015年10月22日
     */
    public static String calcDateDifference(long timeStart, long timeEnd) {
        long between = 0;
        String returnStr = "";
        between = timeEnd - timeStart;// 得到两者的毫秒数

        long second = 1000;// 一秒的毫秒数
        long minute = 60 * second;// 一分钟的毫秒数
        long hour = minute * 60;// 一小时的毫秒数
        long day = hour * 24;// 一天的毫秒数
        long month = day * 30;// 一月的毫秒数
        long year = month * 12;// 一年的毫秒数

        if (between >= 0 && between < second) {
            returnStr = "刚刚";
        } else if (second >= 0 && between < minute) {
            returnStr = between / second + "秒前";
        } else if (between >= minute && between < hour) {
            returnStr = between / minute + "分钟前";
        } else if (between >= hour && between < day) {
            returnStr = between / hour + "小时前";
        } else if (between >= day && between < month) {
            returnStr = between / day + "天前";
        } else if (between >= month && between < year) {
            returnStr = between / month + "个月前";
        } else if (between >= year) {
            returnStr = between / year + "年前";
        }
        return returnStr;
    }

    /**
     * 说明：对两个日期进行比较(long与date)
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDate(Long date1, Date date2) {
        if (date1 == null && date2 == null)
            return true;
        else if (date1 == null && date2 != null)
            return false;
        else if (date1 != null && date2 == null)
            return false;
        else {
            if (date1.longValue() == date2.getTime())
                return true;
            return false;
        }
    }

    /**
     * 说明：对两个日期进行比较(只比较日期，不比较时间)
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int compareOnlyDate(Date date1, Date date2) {
        String date1str = dateTo8String(date1);
        Date newDate1 = convertStr2Date(date1str);
        String date2str = dateTo8String(date2);
        Date newDate2 = convertStr2Date(date2str);
        return newDate1.compareTo(newDate2);
    }

    /**
     * 过滤掉日期中的时分秒，保留年月日
     *
     * @param date
     * @return
     */
    public static Date convertDate2Date(Date date) {
        String dateStr = dateTo8String(date);
        Date adate = convertStr2Date(dateStr);
        return adate;
    }

    /**
     * 将字符串时间转换成Date类型
     *
     * @param str
     * @return
     */
    public static Date convertStr2Date(String str) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(str);
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    /**
     * 将字符串时间转换成Date类型
     *
     * @param str
     * @return
     */
    public static Date convertStr3Date(String str) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(str);
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    public static Date convertStr4Date(String str) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date = sdf.parse(str);
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    /**
     * 比较两个日期相差多少天
     *
     * @param fDate
     * @param oDate
     * @return
     */
    public static int countDiffDateDays(Date fDate, Date oDate) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(fDate);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(oDate);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return (day2 - day1);
    }

    public static long countDiffDateDays2(Date fDate, Date oDate) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTimeInMillis(0);
        aCalendar.setTime(fDate);
        fDate = getDate(aCalendar.get(Calendar.YEAR), aCalendar.get(Calendar.MONTH),
                aCalendar.get(Calendar.DAY_OF_YEAR));
        aCalendar.setTime(oDate);
        oDate = getDate(aCalendar.get(Calendar.YEAR), aCalendar.get(Calendar.MONTH),
                aCalendar.get(Calendar.DAY_OF_YEAR));
        long quot = (oDate.getTime() - fDate.getTime()) / 1000 / 60 / 60 / 24;
        if (quot < 0) {
            quot = -quot;
        }
        return quot;
    }
    
    //比较相差秒数
    public static long countDiffSeconds(Date fDate, Date oDate) {
        long quot = (oDate.getTime() - fDate.getTime()) / 1000;
        if (quot < 0) {
            quot = -quot;
        }
        return quot;
    }

    /**
     * 遍历日期 yyyy-MM-dd
     */
    public static List<Date> dateList(Date s, Date e) {
        // 请注意月份是从0-11
        Calendar start = Calendar.getInstance();
        start.setTime(s);
        Calendar end = Calendar.getInstance();
        end.setTime(e);

        List<Date> dl = new LinkedList<>();
        // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        while (start.compareTo(end) <= 0) {
            // 打印每天
            // System.out.println(format.format(start.getTime()));
            // 循环，每次天数加1
            start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);
            dl.add(start.getTime());
        }

        return dl;
    }

    /**
     * 遍历日期 yyyy-MM-dd
     */
    public static List<Date> dateList(String s, String e) {

        return dateList(DateUtil.StringToDate(s), DateUtil.StringToDate(e));
    }

    /**
     * yyyy-MM-dd HH:mm
     *
     * @param date
     * @return
     */
    public static String dateTo12String(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String dateTo14String(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyy年MM月dd日 HH时mm分ss秒
     *
     * @param date
     * @return
     */
    public static String dateTo14StringCH(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyyMMddHHmmss
     *
     * @param date
     * @return
     */
    public static String dateTo14StrTimestamp(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyyMMdd
     *
     * @param date
     * @return
     */
    public static Integer dateTo8Integer(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        dates = sdf.format(date);
        return Integer.valueOf(dates);
    }

    /**
     * yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateTo8String(Date date) {
        if (date == null) {
            return null;
        }
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyyMMdd
     *
     * @param date
     * @return
     */
    public static String dateTo8String1(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyy/MM/dd
     *
     * @param date
     * @return
     */
    public static String dateTo8String2(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateTo8String3(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * yyyy年MM月dd日 HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String dateTo8StringCH(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        dates = sdf.format(date);
        return dates;
    }

    public static String dateTo8StringCH2(Date date) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
        dates = sdf.format(date);
        return dates;
    }

    /**
     * 说明：格式化时间
     *
     * @param date
     * @param format 格式
     * @return
     */
    public static String dateToFormat(Date date, String format) {
        String dates = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        dates = sdf.format(date);
        return dates;
    }

    /**
     * 方法说明：将util.date转换为Timestamp
     *
     * @param date
     * @return Timestamp
     * @time 2015年9月18日 上午9:46:08
     */
    public static Timestamp dateToTimestamp(Date date) {
        Timestamp timestamp = null;
        if (date == null) {
            return timestamp;
        }
        timestamp = new Timestamp(date.getTime());
        return timestamp;
    }

    /**
     * 相差天数
     */
    public static int differDays(String s, String e) {

        Date start = DateUtil.convertStr2Date(s);
        Date end = DateUtil.convertStr2Date(e);
        long sl = start.getTime();
        long el = end.getTime();

        return new Long((el - sl) / (1000 * 60 * 60 * 24)).intValue();

    }

    public static String formatMin(long min) {
        long diffMinutes = min % 60;
        long diffHours = min / 60 % 24;
        long diffDays = min / (24 * 60);

        if (diffDays > 0) {
            return String.format("%d 天 %d 小时 %d 分", diffDays, diffHours, diffMinutes);
        } else if (diffHours > 0) {
            return String.format("%d 小时 %d 分", diffHours, diffMinutes);
        } else {
            return String.format("%d 分", diffMinutes);
        }

    }

    /**
     * 说明：求两个日期时间差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getBetweenDays(Date date1, Date date2) {
        if (date2.before(date1)) {// 保证date1在date2之前
            Date date3 = date1;
            date1 = date2;
            date2 = date3;
        }
        long to = date1.getTime();
        long from = date2.getTime();
        return (from - to) / (1000 * 60 * 60 * 24);
    }

    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_YEAR, day);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 根据年月日拼装日期
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static Date getDate(String year, String month, String day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = year + "-" + month + "-" + day;
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    /**
     * 今天几号
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        return aCalendar.get(Calendar.DATE);
    }

    /**
     * 获取当前月份
     *
     * @param date
     * @return
     */
    public static Integer getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 说明：根据传入日期获取后一天日期
     *
     * @param date
     * @return
     */
    public static Date getTheDayAfter(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        Date afterDate = calendar.getTime();
        return afterDate;
    }

    /**
     * 说明：根据传入日期获取前一天日期
     *
     * @param date
     * @return
     */
    public static Date getTheDayBefore(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        Date beforeDate = calendar.getTime();
        return beforeDate;
    }

    /**
     * 返回两个时间差 单位秒
     *
     * @param lastTime
     * @param thisTime
     * @return
     * @throws ParseException
     */
    public static Long getTimeR(String lastTime, String thisTime) throws ParseException {

        Long a = new SimpleDateFormat("yyyyMMddHHmmss").parse(lastTime).getTime() / 1000;

        Long b = new SimpleDateFormat("yyyyMMddHHmmss").parse(thisTime).getTime() / 1000;

        return a - b;
    }

    /**
     * 获取日期年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        return aCalendar.get(Calendar.YEAR);
    }

    /**
     * 加减天数
     *
     * @param date
     * @return Date
     */
    public static Date increaseDate(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }

    /**
     * 说明：加减天数并转换为Timestamp 类型时间
     *
     * @param date
     * @param days
     * @return
     */
    public static Timestamp increaseDateAndToTimestamp(Date date, Number days) {
        Timestamp timestamp = null;
        if (date == null) {
            return timestamp;
        }
        int intDays = days.intValue();
        Date increaseDate = DateUtil.increaseDate(new Date(), intDays);
        timestamp = new Timestamp(increaseDate.getTime());
        return timestamp;
    }

    /**
     * 加减小时
     *
     * @param date
     * @return Date
     */
    public static Date increaseHour(Date date, int hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return cal.getTime();
    }

    /**
     * 加减小时
     *
     * @param date
     * @return Date
     */
    public static Date increaseSecond(Date date, int second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, second);
        return cal.getTime();
    }

    /**
     * 时间是否在配置时间之后
     *
     * @param configDate
     * @param entryDate
     * @return
     */
    public static boolean isAfter(Date configDate, Date entryDate) {
        boolean bool = entryDate.after(configDate);
        return bool;
    }

    /**
     * 时间是否在配置时间之前
     *
     * @param configDate
     * @param entryDate
     * @return
     */
    public static boolean isBefore(Date configDate, Date entryDate) {
        boolean bool = entryDate.before(configDate);
        return bool;
    }

    /**
     * 获取当月最后一天
     *
     * @param date
     * @return
     */
    public static Date lastDayByMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = sdf.parse("2015-4-20");
            Date end = sdf.parse("2015-4-20");
            DateUtil.dateList(start, end);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Date now() {
        return new Date();
    }

    /**
     * yyyyMMdd
     *
     * @param str
     * @return
     */
    public static Date string8ToDate(String str) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    /**
     * 字符串转日期
     *
     * @param str
     * @return
     */
    public static Date StringToDate(String str) {
        DateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return formate.parse(str);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 两个日期的时间差
     *
     * @param d1
     * @param d2
     * @return
     */
    public static String timeDiff(Date d1, Date d2) {
        long diff = d2.getTime() - d1.getTime();
        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        if (diffDays > 0) {
            return String.format("%d 天 %d 小时 %d 分 %d 秒", diffDays, diffHours, diffMinutes, diffSeconds);
        } else if (diffHours > 0) {
            return String.format("%d 小时 %d 分 %d 秒", diffHours, diffMinutes, diffSeconds);
        } else if (diffMinutes > 0) {
            return String.format("%d 分 %d 秒", diffMinutes, diffSeconds);
        } else {
            return String.format("%d 秒", diffSeconds);
        }
    }

    /**
     * 两个日期的时间差
     *
     * @param d1
     * @param d2
     * @return
     */
    public static String timeDiff(String d1, String d2) {
        try {
            Date date1 = DateUtil.convertStr3Date(d1);
            Date date2 = DateUtil.convertStr3Date(d2);
            return timeDiff(date1, date2);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 方法说明：将Timestamp转换成string yyyy-MM-dd HH:mm:ss 格式
     *
     * @param timestamp
     * @return String
     * @time 2015年8月24日 上午10:10:25
     */
    public static String timestampTo14String(Timestamp timestamp) {
        if (timestamp == null) {
            return null;
        }
        Date date = new Date(timestamp.getTime());
        String dateStr = dateTo14String(date);
        return dateStr;
    }

    /**
     * 方法说明：将Timestamp转换成string MM-dd 格式
     *
     * @param timestamp
     * @return String
     * @time 2015年8月24日 上午10:10:25
     */
    public static String timestampToSMMdd(Timestamp timestamp) {
        if (timestamp == null) {
            return null;
        }
        Date date = new Date(timestamp.getTime());
        String dateStr = dateToFormat(date, "MM-dd");
        return dateStr;
    }

    /**
     * 判断主方法
     */
    public static boolean validate(String dateString) {
        // 使用正则表达式 测试 字符 符合 dddd-dd-dd 的格式(d表示数字)
        Pattern p = Pattern.compile("\\d{4}+\\d{1,2}+\\d{1,2}+");
        Matcher m = p.matcher(dateString);
        if (!m.matches()) {
            return false;
        }

        // 得到年月日
        int year = Integer.valueOf(dateString.substring(0, 4));
        int month = Integer.valueOf(dateString.substring(4, 6));
        int day = Integer.valueOf(dateString.substring(6, 8));

        if (month < 1 || month > 12) {
            return false;
        }
        int[] monthLengths = new int[]{0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeapYear(year)) {
            monthLengths[2] = 29;
        } else {
            monthLengths[2] = 28;
        }
        int monthLength = monthLengths[month];
        if (day < 1 || day > monthLength) {
            return false;
        }
        return true;
    }

    /**
     * 是否是闰年
     */
    private static boolean isLeapYear(int year) {
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
    }

    /**
     * 获取当前季度的第一天
     */
    public static Date getQuarterFirstDate() {
        Calendar calendar = Calendar.getInstance();
        int currentMonth = calendar.get(Calendar.MONTH);
        int month = 0;
        switch (currentMonth) {
            case 0:
            case 1:
            case 2:
                month = 0;
                break;
            case 3:
            case 4:
            case 5:
                month = 3;
                break;
            case 6:
            case 7:
            case 8:
                month = 6;
                break;
            case 9:
            case 10:
            case 11:
                month = 9;
                break;
        }
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(calendar);
        return calendar.getTime();
    }

    /**
     * 获取当前季度的最后一天
     */
    public static Date getQuarterLastDate() {
        Calendar calendar = Calendar.getInstance();
        int currentMonth = calendar.get(Calendar.MONTH);
        int month = 0;
        switch (currentMonth) {
            case 0:
            case 1:
            case 2:
                month = 2;
                break;
            case 3:
            case 4:
            case 5:
                month = 5;
                break;
            case 6:
            case 7:
            case 8:
                month = 8;
                break;
            case 9:
            case 10:
            case 11:
                month = 11;
                break;
        }
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(calendar);
        return calendar.getTime();
    }

    /**
     * 设置最小时间
     */
    private static void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 设置最大时间
     */
    private static void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }
    /**
	 * 获取当前时间
	 * @return
	 */
	public static String getCreateTime(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime = df.format(new Date());
		return createTime;
	}
	/**
	 * 获取时间戳
	 * @return
	 */
	public static String getTimeStr(){
		long time=System.currentTimeMillis();
		return String.valueOf(time);
	}
	/** 
     * 两个时间相差距离多少天多少小时多少分多少秒 
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00 
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00 
     * @return long[] 返回值为：{天, 时, 分, 秒} 
     */  
    public static long[] getDistanceTimes(String str1, String str2) {  
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date one;  
        Date two;  
        long day = 0;  
        long hour = 0;  
        long min = 0;  
        long sec = 0;  
        try {  
            one = df.parse(str1);  
            two = df.parse(str2);  
            long time1 = one.getTime();  
            long time2 = two.getTime();  
            long diff ;  
            if(time1<time2) {  
                diff = time2 - time1;  
            } else {  
                diff = time1 - time2;  
            }  
            day = diff / (24 * 60 * 60 * 1000);  
            hour = (diff / (60 * 60 * 1000) - day * 24);  
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);  
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        long[] times = {day, hour, min, sec};  
        return times;  
    }  
    /**
     * 比较两个日期
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static int compareMonth(String DATE1, String DATE2) {
        
        DateFormat df = new SimpleDateFormat("yyyyMM");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() < dt2.getTime()) {
                //System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() > dt2.getTime()) {
                //System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    /**
     * 比较两个日期
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static int compareDate(String DATE1, String DATE2) {
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() < dt2.getTime()) {
                //System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() > dt2.getTime()) {
                //System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    /**
     * 获取两个日期相差的天数
     * @param anyday
     * @param nowDate
     * @return
     */
    public static int diffDay(String style,String anyday,Date nowDate){
    	DateFormat format=new SimpleDateFormat(style);
		Date d1=null;
		int diff=0;
		try {
			d1 = format.parse(anyday);
			long sl = d1.getTime();
		    long el = nowDate.getTime();
		    diff= new Long((el - sl) / (1000 * 60 * 60 * 24)).intValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	    return diff;
    }
    /**
     * 获取多少天前的日期
     * @return
     */
    public static String getDiffDay(Date date,int before){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		Calendar now = Calendar.getInstance();  
        now.setTime(date);  
        now.set(Calendar.DATE, now.get(Calendar.DATE) - before);  
        String day=sdf.format(now.getTime());
        return day;
    }
}
